home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 07 - Mechanics - Environment / HexGrid / HexGrid.c < prev    next >
C/C++ Source or Header  |  1995-05-27  |  2KB  |  74 lines

  1.  
  2. /*Size of the array*/
  3. #define    kArraySizeH 10
  4. #define    kArraySizeV 8
  5.  
  6. /* Pictures*/
  7. PicHandle hexTile;
  8.  
  9. /* Define constants that match our hexes */
  10. #define    kHorizontalSpacing    32
  11. #define    kVerticalSpacing    26
  12.  
  13. /* Draw a tile */
  14.  
  15. static void DrawHexTile(short h, short v)
  16. {
  17.     Rect    tileRectangle;
  18.  
  19. /* Use the picture frame */
  20.     tileRectangle = (**hexTile).picFrame;
  21. /* Move it to 0,0 */
  22.     OffsetRect(&tileRectangle, -tileRectangle.left,  -tileRectangle.top);
  23. /* Offset to the proper position */
  24. /* For every other line, offset a bit extra */
  25.     if ((v & 1) == 0)
  26.         OffsetRect(&tileRectangle, kHorizontalSpacing*h, kVerticalSpacing*v);
  27.     else
  28.         OffsetRect(&tileRectangle, kHorizontalSpacing*h + kHorizontalSpacing / 2, kVerticalSpacing*v);
  29. /* Draw it */    
  30.     DrawPicture(hexTile, &tileRectangle);
  31. } /*DrawHexTile*/
  32.  
  33.  
  34. /* Standard inits */
  35.  
  36. static void InitToolbox(void) {
  37.     InitGraf (&qd.thePort);
  38.     InitFonts ();
  39.     FlushEvents (everyEvent,0);
  40.     InitWindows ();
  41.     InitMenus ();
  42.     TEInit ();
  43.     InitDialogs (nil);
  44.     InitCursor ();
  45. }
  46.  
  47. /****************** Main program ******************/
  48.  
  49. void main(void) {
  50.     WindowPtr    myWindow;
  51.     Rect    windowRectangle;
  52.     short    h,v;
  53.  
  54.     InitToolbox();
  55.  
  56. /*Set up the window*/
  57.     SetRect(&windowRectangle, 50, 50,
  58.                 50 + kArraySizeH * kHorizontalSpacing + kHorizontalSpacing / 2,
  59.                 50 + kArraySizeV * kVerticalSpacing + kVerticalSpacing / 3);
  60.     myWindow = NewCWindow(nil, &windowRectangle, "\pHex grid demo", true, 0, (WindowPtr)-1L, false, 0);
  61.     SetPort(myWindow);
  62.  
  63. /*Load the picture*/
  64.     hexTile = GetPicture(128);            /*PICT resource #128.*/
  65.  
  66. /*Draw all tiles!*/
  67.     for ( h = 0 ; h < kArraySizeH ; h++)
  68.         for ( v = 0 ; v < kArraySizeV ; v++)
  69.             DrawHexTile(h, v);
  70.  
  71.     while ( ! Button () )
  72.         ;
  73. } /*TextGrid*/
  74.